package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.message.vos.SearchArticleVo;
import com.heima.utils.common.SnowflakeIdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 根据参数加载文章列表
     *
     * @param loadtype 1为加载更多  2为加载最新
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {
        //1.参数校验
            //分页size
        if(dto.getSize()==null){
            dto.setSize(10);
        }
            //频道id
        if(dto.getTag()==null){
            dto.setTag("__all__");
        }
            //最小时间，对标是当前页最后一条数据的发布时间
        if(dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
            //最大时间，对标是当前页第一条数据的发布时间
        if(dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }

        //2.调用mapper接口
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, loadtype);

        //3.返回数据
        return ResponseResult.okResult(apArticles);
    }


    /**
     * 保存文章内容+文章配置+文章信息
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult save(ArticleDto dto) {
        ApArticle apArticle=new ApArticle();
        //1.判断保存文章还是修改
        if(dto.getId()==null){//保存
            //新增ap_article
            BeanUtils.copyProperties(dto,apArticle);
            apArticle.setCreatedTime(new Date());
            save(apArticle);

            //新增ap_article_config
            ApArticleConfig config=new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(config);

            //新增ap_article_content
            ApArticleContent aparticleContent=new ApArticleContent();
            aparticleContent.setArticleId(apArticle.getId());
            aparticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(aparticleContent);
        }else{//修改
            //1.先根据id去查询文章，在修改
            apArticle = apArticleMapper.selectById(dto.getId());
            if(apArticle!=null){
                BeanUtils.copyProperties(dto,apArticle);
                apArticleMapper.updateById(apArticle);
                //先查文章内容，在修改
                ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                        .eq(ApArticleContent::getArticleId, apArticle.getId()));

                apArticleContent.setContent(dto.getContent());

                apArticleContentMapper.updateById(apArticleContent);
            }
        }

        //异步调用生成静态文件保存到minio中
        articleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());




        return ResponseResult.okResult(apArticle.getId());
    }




    /**
     * 只是为了当前首页查询的时候查询redis
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load2(ArticleHomeDto dto) {
        //查询redis
        String hotArticleVoStr = redisTemplate.opsForValue().get(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + dto.getTag());
        //转成对象
        List<HotArticleVo> hotArticleVoList = JSONArray.parseArray(hotArticleVoStr, HotArticleVo.class);
        return ResponseResult.okResult(hotArticleVoList);
    }

}
